package org.dynamo.database.ui.wizzard.wizards;

/*******************************************************************************
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jean Neveux - initial API and implementation
 *******************************************************************************/

import org.dynamo.database.ui.wizzard.helper.DatabaseHelper;
import org.eclipse.datatools.modelbase.sql.schema.Catalog;
import org.eclipse.datatools.modelbase.sql.schema.Database;
import org.eclipse.datatools.modelbase.sql.schema.Schema;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;

public class DatabaseConnectionPage extends WizardPage {

	private List dbsList;
	private List dbsListCatalog;
	private List dbsListSchema;
	private Database[] dbs;
	private Database selectedDbs;
	private Catalog selectedCatalog;
	private Schema selectedSchema;

	protected DatabaseConnectionPage(String pageName) {
		super(pageName);
		setTitle("Enter the database connection info (v1.0.402)");
	}

	@Override
	public void createControl(Composite parent) {

		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;

		Label labelSelectDbs = new Label(container, SWT.NULL);
		labelSelectDbs.setText("Select database:");
		dbsList = new List(container, SWT.SIMPLE | SWT.BORDER | SWT.V_SCROLL);
		GridData dgA = new GridData(GridData.FILL_BOTH);
		dgA.heightHint = 100;
		dbsList.setLayoutData(dgA);
		dbsList.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {

			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				selectDatabase();
			}

		});

		Label labelSelectCatalog = new Label(container, SWT.NULL);
		labelSelectCatalog.setText("Select catalog: ");
		dbsListCatalog = new List(container, SWT.SIMPLE | SWT.BORDER
				| SWT.V_SCROLL);
		dbsListCatalog.setEnabled(false);
		GridData dgC = new GridData(GridData.FILL_BOTH);
		dgC.heightHint = 100;
		dbsListCatalog.setLayoutData(dgC);
		dbsListCatalog.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				selectCatalog();
			}
		});

		Label labelSelectSchema = new Label(container, SWT.NULL);
		labelSelectSchema.setText("Select schema:");
		dbsListSchema = new List(container, SWT.SIMPLE | SWT.BORDER
				| SWT.V_SCROLL);
		dbsListSchema.setEnabled(false);
		GridData dgS = new GridData(GridData.FILL_BOTH);
		dgS.heightHint = 100;
		dbsListSchema.setLayoutData(dgS);
		dbsListSchema.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			@Override
			public void widgetSelected(SelectionEvent e) {
				selectSchema();
			}
		});

		initialize();
		dialogChanged();
		setControl(container);

	}

	protected void selectSchema() {
		int index = 0;
		if (selectedCatalog != null
				&& !selectedCatalog.getSchemas().isEmpty()) {
			for (Object obj : selectedCatalog.getSchemas()) {
				Schema sc = (Schema) obj;
				if (dbsListSchema.getSelectionIndex() == index) {
					selectedSchema = sc;
				}
				index++;
			}
		} else {
			for (Object obj : selectedDbs.getSchemas()) {
				Schema sc = (Schema) obj;
				if (dbsListSchema.getSelectionIndex() == index) {
					selectedSchema = sc;
				}
				index++;
			}
		}
		dialogChanged();
	}

	protected void selectCatalog() {
		int index = 0;
		dbsListSchema.setItems(new String[] {});
		for (Object obj : selectedDbs.getCatalogs()) {
			Catalog ct = (Catalog) obj;
			if (dbsListCatalog.getSelectionIndex() == index) {
				dbsListSchema.setEnabled(true);
				selectedCatalog = ct;
			}
			index++;
		}
		if (selectedCatalog != null) {
			for (Object obj : selectedCatalog.getSchemas()) {
				Schema sc = (Schema) obj;
				dbsListSchema.add(sc.getName());
			}
		} else {
			for (Object obj : selectedDbs.getSchemas()) {
				dbsListSchema.setEnabled(true);
				Schema sc = (Schema) obj;
				dbsListSchema.add(sc.getName());
			}
		}
		dialogChanged();
	}

	private void selectDatabase() {
		selectedDbs = DatabaseHelper.getInstace().getDbs()[dbsList
				.getSelectionIndex()];
		dbsListCatalog.setEnabled(true);
		dbsListCatalog.setItems(new String[] {});
		dbsListSchema.setItems(new String[] {});
		for (Object obj : selectedDbs.getCatalogs()) {
			Catalog ct = (Catalog) obj;
			dbsListCatalog.add(ct.getName());
		}
		if (dbsListCatalog.getItemCount() == 0) {
			dbsListCatalog.add("default");
		} else if ("".equals(dbsListCatalog.getItem(0))) {
			dbsListCatalog.setItem(0, "default");
		}
		dialogChanged();
	}

	private void dialogChanged() {
		if (selectedSchema != null) {
			updateStatus(null);
		} else {
			updateStatus("You must select a database, a catalog and a schema.");
		}
	}

	private void initialize() {

		DatabaseHelper.getInstace().connectAll();

		DatabaseHelper.getInstace().resetDatabase();

		dbs = DatabaseHelper.getInstace().getDbs();
		if (dbs.length == 0) {
			updateStatus("You must create a data profile using the Data Tools Plateform.");
		}
		selectedCatalog = null;
		selectedDbs = null;
		selectedSchema = null;
		for (Database db : dbs) {
			dbsList.add(db.getName() + " : " + db.getDescription());
		}

	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	public Database getSelectedDbs() {
		return selectedDbs;
	}

	public Catalog getSelectedCatalog() {
		return selectedCatalog;
	}

	public Schema getSelectedSchema() {
		return selectedSchema;
	}
}
